package com.gesee.zyspps.widgets;

import com.gesee.zys.R;
import com.gesee.zyspps.views.ButtonFlat;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * @ClassName: MaterialDialog
 * @Description: TODO(弹窗工具类)
 * @author fujianxiong
 * @date 2015年8月11日 下午3:18:55
 *
 */
public class MaterialDialog extends Dialog
{

    Context context;
    RelativeLayout view;
    View backView;
    RelativeLayout relative;
    View middleContentView;
    View contentView;
    TextView messageTextView;
    String title;
    String message;
    boolean isTrue = true;
    TextView titleTextView;

    Button buttonAccept;
    Button buttonCancel;

    View.OnClickListener onAcceptButtonClickListener;
    View.OnClickListener onCancelButtonClickListener;

    public MaterialDialog(Context context, String title, String message)
    {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;// init Context
        this.message = message;
        this.title = title;
    }

    public MaterialDialog(Context context, String title, String message, boolean isTure)
    {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;// init Context
        this.message = message;
        this.title = title;
        this.isTrue = isTure;
    }

    public MaterialDialog(Context context, String title, View middleContentView)
    {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;// init Context
        this.middleContentView = middleContentView;
        this.title = title;
    }

    public MaterialDialog(Context context, String title, View middleContentView, boolean isTure)
    {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;// init Context
        this.middleContentView = middleContentView;
        this.title = title;
        this.isTrue = isTure;
    }

    public MaterialDialog(Context context, View contentView)
    {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;// init Context
        this.contentView = contentView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meterial_dialog);
        backView = (RelativeLayout) findViewById(R.id.dialog_headView);
        view = (RelativeLayout) findViewById(R.id.contentDialog);

        backView.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getX() < view.getLeft() || event.getX() > view.getRight() || event.getY() > view.getBottom() || event.getY() < view.getTop())
                {
                    if (isTrue == false)
                    {
                        return false;
                    }
                    else
                    {
                        dismiss();
                    }

                }
                return false;
            }
        });

        if (null != contentView)
        {
            view.removeAllViews();
            view.addView(contentView);
            return;
        }

        if (null != middleContentView)
        {
            view.removeAllViews();
            // view.addView(middleContentView);
            setContentView(R.layout.meterial_view_dialog);
            view = (RelativeLayout) findViewById(R.id.contentDialog);
            RelativeLayout mView = (RelativeLayout) findViewById(R.id.view);
            mView.addView(middleContentView);
            backView = (RelativeLayout) findViewById(R.id.dialog_rootView);
            backView.setOnTouchListener(new OnTouchListener()
            {

                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    if (event.getX() < view.getLeft() || event.getX() > view.getRight() || event.getY() > view.getBottom() || event.getY() < view.getTop())
                    {
                        if (isTrue == false)
                        {
                            return false;
                        }
                        else
                        {
                            dismiss();
                        }
                    }
                    return false;
                }
            });

            this.titleTextView = (TextView) findViewById(R.id.title);
            setTitle(title);
            this.buttonAccept = (Button) findViewById(R.id.button_accept);
            buttonAccept.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
//                    dismiss();
                    if (onAcceptButtonClickListener != null)
                        onAcceptButtonClickListener.onClick(v);
                }
            });
            this.buttonCancel = (Button) findViewById(R.id.button_cancel);
            buttonCancel.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    dismiss();
                    if (onCancelButtonClickListener != null)
                        onCancelButtonClickListener.onClick(v);
                }
            });
            return;
        }

        this.titleTextView = (TextView) findViewById(R.id.title);
        setTitle(title);

        this.messageTextView = (TextView) findViewById(R.id.message);
        setMessage(message);

        this.buttonAccept = (Button) findViewById(R.id.button_accept);
        buttonAccept.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
//                dismiss();
                if (onAcceptButtonClickListener != null)
                    onAcceptButtonClickListener.onClick(v);
            }
        });
        this.buttonCancel = (Button) findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                dismiss();
                if (onCancelButtonClickListener != null)
                    onCancelButtonClickListener.onClick(v);
            }
        });
    }

    @Override
    public void show()
    {
        // TODO 自动生成的方法存根
        super.show();
        // set dialog enter animations
        view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_main_show_amination));
        backView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_root_show_amin));
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
        messageTextView.setText(message);
    }

    public TextView getMessageTextView()
    {
        return messageTextView;
    }

    public void setMessageTextView(TextView messageTextView)
    {
        this.messageTextView = messageTextView;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
        if (title == null)
            titleTextView.setVisibility(View.GONE);
        else
        {
            titleTextView.setVisibility(View.VISIBLE);
            titleTextView.setText(title);
        }
    }

    public TextView getTitleTextView()
    {
        return titleTextView;
    }

    public void setTitleTextView(TextView titleTextView)
    {
        this.titleTextView = titleTextView;
    }

    public Button getButtonAccept()
    {
        return buttonAccept;
    }

    public void setButtonAccept(Button buttonAccept)
    {
        this.buttonAccept = buttonAccept;
    }

    public Button getButtonCancel()
    {
        return buttonCancel;
    }

    public void setButtonCancel(Button buttonCancel)
    {
        this.buttonCancel = buttonCancel;
    }

    public void setOnAcceptButtonClickListener(View.OnClickListener onAcceptButtonClickListener)
    {
        this.onAcceptButtonClickListener = onAcceptButtonClickListener;
        if (buttonAccept != null)
            buttonAccept.setOnClickListener(onAcceptButtonClickListener);
    }

    public void setOnCancelButtonClickListener(View.OnClickListener onCancelButtonClickListener)
    {
        this.onCancelButtonClickListener = onCancelButtonClickListener;
        if (buttonCancel != null)
            buttonCancel.setOnClickListener(onAcceptButtonClickListener);
    }

    @Override
    public void dismiss()
    {
        Animation anim = AnimationUtils.loadAnimation(context, R.anim.dialog_main_hide_amination);
        anim.setAnimationListener(new AnimationListener()
        {

            @Override
            public void onAnimationStart(Animation animation)
            {
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                view.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        MaterialDialog.super.dismiss();
                    }
                });

            }
        });
        Animation backAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_root_hide_amin);

        view.startAnimation(anim);
        backView.startAnimation(backAnim);
    }

}
